package com.briup.smartlabs.web.controller;

import com.briup.smartlabs.bean.SStu;
import com.briup.smartlabs.mapper.SStuMapper;
import com.briup.smartlabs.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sStu")
public class SStuController {
    @Autowired
    private SStuMapper sStuMapper;
    @GetMapping("/{sno}")
    public Result<SStu> getSStuById(@PathVariable Long sno){
        // 手动创造一个系统的运行时异常
        //int i = 10 / 0;

        //手动抛出一个自定义异常
       // if (1>0) {
      //      throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
     //   }
        SStu sStu=sStuMapper.selectById(sno);
        return Result.success(sStu);
    }


    @GetMapping("/condition")
    public Result<List<SStu>> getSStuByCondition(@RequestParam String name,
                                                 @RequestParam Integer age,
                                                 @RequestParam String gender){
        List<SStu> list=sStuMapper.selectByCondition(name,age,gender);
        return Result.success(list);
    }
    @PostMapping("/")
    public Result<String> saveSStu(@RequestBody SStu sStu){
        sStuMapper.insert(sStu);
        return Result.success("添加成功");
    }
    @PutMapping("/")
    public Result<?> updateSStu(@RequestBody SStu sStu){
        sStuMapper.update(sStu);
        return Result.success();
    }
    @DeleteMapping("/{sno}")
    public Result<?> removeSStuById(@PathVariable Long sno){
        sStuMapper.deleteById(sno);
        return Result.success();
    }
    @DeleteMapping("/batch")
    public Result<?> removeBatch(@RequestBody List<Long> sno){
        sStuMapper.deleteBatch(sno);
        return Result.success();
    }
}
